{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.41cb2ecccad4b8d8fd91b3411968e18d.315360000.1971509634.282335-26545900', 'expires_in': 2592000, 'session_key': '9mzdWWkN93ScyY62qYaSty53eg3EmBTueUUWMqadX/Z2+8M5vUsBdwfGqWYQ1emxcXUI8+6RzOhVaBaWJh0Bc4h5aGStvA==', 'access_token': '24.b3ff4b0d20dbccad69096d2ea3befe25.2592000.1658741634.282335-26545900', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '02c12571d91ea31c58f3fda0f4b0995c'}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=n4krxG3NPR4QO9oEgLL7iex6&client_secret=dr9Dp0QcXLF1AeMhyFRQC0ZCt9vEgOjB'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.82929474, 'name': '服装饰品', 'location': {'top': 468, 'left': 655, 'width': 23, 'height': 21}}, {'score': 0.15704541, 'name': '服装饰品', 'location': {'top': 464, 'left': 505, 'width': 24, 'height': 16}}, {'score': 0.80677617, 'name': '鞋品箱包', 'location': {'top': 665, 'left': 522, 'width': 26, 'height': 15}}, {'score': 0.6272604, 'name': '鞋品箱包', 'location': {'top': 520, 'left': 346, 'width': 33, 'height': 48}}, {'score': 0.1430385, 'name': '鞋品箱包', 'location': {'top': 649, 'left': 359, 'width': 21, 'height': 32}}, {'score': 0.5920213, 'name': '鞋品箱包', 'location': {'top': 657, 'left': 499, 'width': 16, 'height': 21}}, {'score': 0.9563202, 'name': '鞋品箱包', 'location': {'top': 573, 'left': 676, 'width': 31, 'height': 69}}, {'score': 0.60257965, 'name': '鞋品箱包', 'location': {'top': 562, 'left': 468, 'width': 27, 'height': 33}}, {'score': 0.64563555, 'name': '鞋品箱包', 'location': {'top': 635, 'left': 659, 'width': 17, 'height': 26}}, {'score': 0.93963546, 'name': '鞋品箱包', 'location': {'top': 555, 'left': 342, 'width': 68, 'height': 73}}, {'score': 0.52448046, 'name': '鞋品箱包', 'location': {'top': 569, 'left': 538, 'width': 15, 'height': 43}}, {'score': 0.46257868, 'name': '鞋品箱包', 'location': {'top': 564, 'left': 613, 'width': 25, 'height': 31}}, {'score': 0.41682607, 'name': '家居家纺', 'location': {'top': 225, 'left': 945, 'width': 25, 'height': 20}}, {'score': 0.11867769, 'name': '家居家纺', 'location': {'top': 396, 'left': 797, 'width': 25, 'height': 18}}, {'score': 0.14349154, 'name': '家居家电', 'location': {'top': 405, 'left': 707, 'width': 69, 'height': 206}}, {'score': 0.5848154, 'name': '文化娱乐', 'location': {'top': 504, 'left': 836, 'width': 54, 'height': 60}}, {'score': 0.108400404, 'name': '文化娱乐', 'location': {'top': 458, 'left': 768, 'width': 46, 'height': 144}}, {'score': 0.5607373, 'name': '文化娱乐', 'location': {'top': 521, 'left': 894, 'width': 56, 'height': 78}}, {'score': 0.13099818, 'name': '文化娱乐', 'location': {'top': 352, 'left': 932, 'width': 63, 'height': 164}}, {'score': 0.31278455, 'name': '文化娱乐', 'location': {'top': 425, 'left': 848, 'width': 89, 'height': 88}}, {'score': 0.8615374, 'name': '植物', 'location': {'top': 488, 'left': 553, 'width': 45, 'height': 79}}, {'score': 0.29903024, 'name': '植物', 'location': {'top': 586, 'left': 763, 'width': 95, 'height': 90}}, {'score': 0.8218061, 'name': '人', 'location': {'top': 452, 'left': 446, 'width': 39, 'height': 123}}, {'score': 0.79409134, 'name': '人', 'location': {'top': 459, 'left': 306, 'width': 35, 'height': 119}}, {'score': 0.5386399, 'name': '人', 'location': {'top': 457, 'left': 408, 'width': 33, 'height': 97}}, {'score': 0.50923485, 'name': '人', 'location': {'top': 463, 'left': 531, 'width': 29, 'height': 100}}, {'score': 0.3427848, 'name': '人', 'location': {'top': 478, 'left': 297, 'width': 14, 'height': 41}}, {'score': 0.24112144, 'name': '人', 'location': {'top': 442, 'left': 278, 'width': 393, 'height': 239}}], 'log_id': 1540629415403227475}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/multi_object_detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open(r'D:\\API\\final_api\\hutong.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '24.b3ff4b0d20dbccad69096d2ea3befe25.2592000.1658741634.282335-26545900'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
